package com.supermap.business.product;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.supermap.business.cloud.push.MyWebSocket;
import com.supermap.business.push.getui.AppPush;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.agrinfo.crop.CropService;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;
import com.supermap.dubbo.api.cloud.suggest.Suggest;
import com.supermap.dubbo.api.cloud.suggest.SuggestService;
import com.supermap.dubbo.api.product.ExpertProductService;
import com.supermap.dubbo.api.product.entity.ExpertProduct;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:zhoujian
 * @Desciption:
 * @date:2019-04-08 16:42
 * @version:v1.0.0
 */
@RestController
@RequestMapping("expert")
public class ExpertProductController {

    @Reference(timeout = 100000)
    SuggestService suggestService;
    @Reference(timeout = 100000)
    private PushService pushService;
    @Reference(timeout = 100000)
    private CropService cropService;


    @Reference(timeout = 10000)
    private ExpertProductService expertProductService;

    @GetMapping("getProductListByUserId")
    public JSONUtils selectSuggestByUserId(@RequestParam(value = "userId") String userId ,
                                           @RequestParam(value="type") Integer type,
                                           @RequestParam(value="pageNum") Integer pageNum ,
                                           @RequestParam(value="pageSize") Integer pageSize)
    {
        return  suggestService.getProductListByUserId(userId,type,pageNum,pageSize);
    }
    @GetMapping("getProductDetails")
    public JSONUtils getSuggestDetails(@RequestParam(value = "productId") Integer productId)
    {
        return  suggestService.getProductDetails(productId);
    }

    /**
     * 添加专家经验性产品
     * 并推送用户
     */
    @RequestMapping(value = "insertExpertProduct" , method = RequestMethod.POST)
    public JSONUtils insertExpertProduct(@RequestBody JSONObject experProduct)
    {
        System.out.println(experProduct);
        String userIds = experProduct.getString("userIds");
        experProduct.remove("userIds");
        Integer expertUserId = experProduct.getInteger("userId");
        JSONUtils jsonUtils = expertProductService.insertExpertProduct(experProduct);
        ExpertProduct expertProductObject = (ExpertProduct) jsonUtils.getData();

        Suggest suggest = new Suggest();
        suggest.setUserid(expertUserId);
        suggest.setAreacode(expertProductObject.getAreaCode());
        suggest.setCrop(expertProductObject.getCropsId());
        suggest.setPubertyId(expertProductObject.getCropsPubertyId());
        suggest.setTypeid(expertProductObject.getProductType());

        JSONUtils jsonObject = cropService.getSubjectByCrop(expertProductObject.getProductType());
        Object object = jsonObject.getData();
        if (object != null)
        {
            suggest.setSubjectid(object.toString());
        }else
        {
            suggest.setSubjectid(null);
        }
        suggest.setTitle(expertProductObject.getTitle());
        suggest.setInfo(expertProductObject.getContent());
        JSONUtils suggestObject = suggestService.insertSuggest(suggest);
        if (suggestObject != null && suggestObject.getCode() == 200 && suggestObject.getData() != null)
        {
            Suggest suggest1 = (Suggest) suggestObject.getData();
            String noticetype = pushService.getProductNoticeTypes("quanguoapp2019131xsadwqeq");
            String[] userIdArray = userIds.split(",");
            Notice notice = new Notice();
            String dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            notice.setUploadtime(dateTime);
            notice.setNoticetype(2);
            notice.setMessageid(suggest1.getId());
            notice.setNoticetitle(suggest1.getTitle());
            //发送者
            notice.setSenduserid(expertProductObject.getExpertId().toString());
            String userIDs  = "";
            for (String userId : userIdArray)
            {
                userIDs+=userId+",";
                //接收者
                notice.setReceiveuserid(userId);
                pushService.putNotice(notice);
                //总数推送
                MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(userId,noticetype).getJsonStr(),userId);
            }
            userIDs=userIDs.substring( 0,userIDs.lastIndexOf(","));
            List<String> strings=pushService.getUserCode(userIDs);
            AppPush.UserCodePush(strings,"有一位专家向您推送了一个专家产品",notice.getNoticetitle());
        }
        return jsonUtils;
    }

    @RequestMapping(value = "queryByCropAndTypeList" , method = RequestMethod.GET)
    public JSONUtils queryByCropAndTypeList(@RequestParam("productType") Integer productType, String cropType, String cropsId, Integer cropsPubertyId)
    {
        return  expertProductService.queryByCropAndTypeList(productType,cropType,cropsId,cropsPubertyId);
    }
}